﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using Faz.SideWinderSC.Logic;

namespace Faz.SideWinderSC.Client.ViewModel
{
    /// <summary>
    /// Provides a lazy-initialized collection that can be used as part of the view-model layer.
    /// </summary>
    /// <typeparam name="T">The type of the elements in the collection.</typeparam>
    public sealed class ViewModelCollection<T> : LazyCollection<T>, INotifyCollectionChanged, INotifyPropertyChanged
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelCollection{T}"/> class
        /// without initializer.
        /// </summary>
        public ViewModelCollection()
            : this(null)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelCollection{T}"/> class
        /// with a specific initializer.
        /// </summary>
        /// <param name="initialize">
        /// The initializer of the collection.
        /// </param>
        public ViewModelCollection(Func<IEnumerable<T>> initialize)
            : base(new ObservableCollection<T>(), initialize)
        { }

        /// <summary>
        /// Occurs when the collection changes.
        /// </summary>
        public event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add { ((INotifyCollectionChanged)this.Encapsulated).CollectionChanged += value; }
            remove { ((INotifyCollectionChanged)this.Encapsulated).CollectionChanged -= value; }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { ((INotifyPropertyChanged)this.Encapsulated).PropertyChanged += value; }
            remove { ((INotifyPropertyChanged)this.Encapsulated).PropertyChanged -= value; }
        }
    }
}
